﻿#include "baserole.h"

#include <QtAlgorithms>

BaseRole::BaseRole(RoleType type)
    : mRoleType(type),
      mAngle(0.0f),
      mLastMapTile(-1),
      mCurMapTile(-1),
      mMapIndex(-1),
      mClient(NULL),
      mRoleState(ROLESTATE_WAITING),
      mID(-1),
      mEscaperate(0),
      mRoleStateStartTime(0),
      mWaokingAngle(0.0f),
      mWalkingMaxTime(0),
      mBlood(100),
      msrcBlood(0),
      mDefense(0),
      mExperience(0),
      mGrade(0),
      mStateIsChanged(false),
      mAngleIsChanged(false),
      mPosIsChanged(false),
      mSelfhealing(0.0f),
      mSelfhealingtotal(0.0f)
{

}

BaseRole::~BaseRole()
{
    clearRoleList();
}

/**
 * @brief BaseRole::addRole 添加一个角色到可视角色列表中
 * @param role
 * @return
 */
bool BaseRole::addRole(BaseRole *role)
{
    if(role == NULL || isExistRole(role))
        return false;

    mViewRoleList.push_front(role);

    return true;
}

/**
 * @brief BaseRole::addRoles 添加一组角色到可视列表中
 * @param roles
 * @return
 */
bool BaseRole::addRoles(QList<BaseRole*> roles)
{
    if(roles.isEmpty()) return false;

    QList<BaseRole*>::iterator iter = roles.begin();
    for(;iter != roles.end();iter++)
    {
        this->addRole((*iter));
    }

    return true;
}

/**
 * @brief BaseRole::delRoleNoRect 清除不在指定范围内的所有角色
 * @param rect
 * @return
 */
QList<BaseRole*> BaseRole::delRoleNoRect(QRect rect)
{
    QList<BaseRole*> returnRoles;

    QList<BaseRole*>::iterator iter = mViewRoleList.begin();
    for(;iter != mViewRoleList.end();)
    {
        if(!rect.contains((*iter)->getPosition()))
        {
            returnRoles.push_back((*iter));
            iter = mViewRoleList.erase(iter);
        }
        else
            iter++;
    }

    return returnRoles;
}

/**
 * @brief BaseRole::delRoleFromViewList 从可视角色列表中删除指定角色
 * @param role
 * @return
 */
bool BaseRole::delRoleFromViewList(BaseRole *role)
{
    if(role == NULL)
        return false;

    QList<BaseRole*>::iterator iter = qFind(mViewRoleList.begin(),
                                            mViewRoleList.end(),
                                            role);
    if(iter != mViewRoleList.end())
        mViewRoleList.erase(iter);

    return true;
}

/**
 * @brief BaseRole::getViewRoleList 得到当前角色可以看见所有其它角色
 * @param type
 * @return
 */
QList<BaseRole*> BaseRole::getViewRoleList(RoleType type)
{
    if(type == ROLETYPE_NULL)
        return mViewRoleList;

    QList<BaseRole*> returnRoles;

    QList<BaseRole*>::iterator iter = mViewRoleList.begin();
    for(;iter != mViewRoleList.end();++iter)
    {
        if((*iter)->getRoleType() != type)
            continue;

        returnRoles.push_back((*iter));
    }

    return returnRoles;
}

/**
 * @brief BaseRole::clearRoleList 清空可视角色列表
 */
void BaseRole::clearRoleList(void)
{
    mViewRoleList.clear();
}

/**
 * @brief BaseRole::setState 设置活动状态
 * @param state
 */
void BaseRole::setState(RoleState state)
{
    mRoleState = state;
    mRoleStateStartTime = QDateTime::currentMSecsSinceEpoch();
}

/**
 * @brief BaseRole::getStateTime 得到当前状态保持时间
 * @return
 */
qint64 BaseRole::getStateTime(void)
{
    return QDateTime::currentMSecsSinceEpoch() - mRoleStateStartTime;
}

/**
 * @brief BaseRole::isExistRole 检测指定角色是否存在列表中
 * @param role
 * @return
 */
bool BaseRole::isExistRole(BaseRole *role)
{
    if(role == NULL) return false;

    QList<BaseRole*>::iterator iter = qFind(mViewRoleList.begin(),
                                            mViewRoleList.end(),
                                            role);
    if(iter == mViewRoleList.end())
        return false;

    return true;
}
